# Maintainer: Peter Budai <peterbudai@hotmail.com>

_realname=dlib
pkgbase="mingw-w64-${_realname}"
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}" "${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=19.24.2
pkgrel=2
pkgdesc="A toolkit for making real world machine learning and data analysis applications in C++ (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
url='http://dlib.net/'
license=('spdx:BSL-1.0')
depends=("${MINGW_PACKAGE_PREFIX}-giflib"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-sqlite3")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
source=(${_realname}-${pkgver}.tar.gz::"https://github.com/davisking/${_realname}/archive/v${pkgver}.tar.gz"
        "001-python-binding.patch")
sha256sums=('0f5c7e3de6316a513635052c5f0a16a84e1cef26a7d233bf00c21348462b6d6f'
            'b2f41b3a7663b29cf2141900508bc90bb21ad5d972eec190b1ffd3fd75aa0a9e')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -Np1 -i "${srcdir}/001-python-binding.patch"

  cd "${srcdir}"
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}"
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -G'Ninja' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${_extra_config[@]}" \
      -DUSE_AVX_INSTRUCTIONS=ON \
      -DDLIB_ISO_CPP_ONLY=OFF \
      -DDLIB_IN_PROJECT_BUILD=OFF \
      -DDLIB_USE_CUDA=OFF \
      -DDLIB_JPEG_SUPPORT=ON \
      -DDLIB_PNG_SUPPORT=ON \
      -DDLIB_GIF_SUPPORT=ON \
      -DDLIB_LINK_WITH_SQLITE3=ON \
      -DDLIB_USE_FFTW=ON \
      -DBUILD_SHARED_LIBS=ON \
      -DBLA_VENDOR=OpenBLAS \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build .

  msg "Python build for ${MSYSTEM}"
  cd "${srcdir}/python-build-${MSYSTEM}"
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/python setup.py build \
    -G "Ninja" \
    --set CMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    --set USE_AVX_INSTRUCTIONS=ON \
    --set DLIB_ISO_CPP_ONLY=OFF \
    --set DLIB_IN_PROJECT_BUILD=ON \
    --set DLIB_USE_CUDA=OFF \
    --set DLIB_JPEG_SUPPORT=ON \
    --set DLIB_PNG_SUPPORT=ON \
    --set DLIB_GIF_SUPPORT=ON \
    --set DLIB_LINK_WITH_SQLITE3=ON \
    --set DLIB_USE_FFTW=ON \
    --set BLA_VENDOR=OpenBLAS=ON
}

#check() {
  #cd "${srcdir}"/build-${MSYSTEM}
  #make check
#}

package_dlib() {
  optdepends=("${MINGW_PACKAGE_PREFIX}-python-dlib")
  cd "${srcdir}"/build-${MSYSTEM}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install ./

  # Remove hard coded library path from cmake files
  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})
  for pcfile in  "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" -i "${pcfile}"
  done
  pushd "${pkgdir}${MINGW_PREFIX}/lib/cmake/dlib" > /dev/null
  sed -s "s|${PREFIX_DEPS}/include||g" -i ./dlibConfig.cmake
  sed -s -e "s|${PREFIX_DEPS}/lib/||g" -e "s|${PREFIX_DEPS}/include||g" -i ./dlib.cmake
  popd > /dev/null
}

package_python-dlib() {
  depends=("${MINGW_PACKAGE_PREFIX}-dlib"
           "${MINGW_PACKAGE_PREFIX}-python")
  optdepends=("${MINGW_PACKAGE_PREFIX}-python-numpy: Python interface")
  provides=("${MINGW_PACKAGE_PREFIX}-python3-dlib")
  conflicts=("${MINGW_PACKAGE_PREFIX}-python3-dlib")
  replaces=("${MINGW_PACKAGE_PREFIX}-python3-dlib")

  cd "${srcdir}/python-build-${MSYSTEM}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}" --optimize=1 --skip-build
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
